import { http } from 'utils'
import * as types from '../mutation-types'

const state = {
  indexBannerList: [],
  creditBannerList: [],
  updateMsg: '',
}

const actions = {
  async getBannerList({ commit }, type) {
    const res = await http.get('banner/list', { type })
    if (res.code === 1) {
      const data = res.data.data
      commit(types.GET_BANNER_LIST_SUCCESS, {
        data,
        type,
      })
      return data
    }
    throw new Error(res.info)
  },

  async updateBanner({ commit }, payload) {
    const res = await http.post('banner/update', payload)
    if (res.code === 1) {
      commit(types.UPDATE_BANNER_SUCCESS, res)
      return res.data.data
    }
    throw res.info
  },
}

const mutations = {
  [types.UPDATE_BANNER_SUCCESS](state, res) {
    res.updateMsg = res.info
  },

  [types.GET_BANNER_LIST_SUCCESS](state, { data, type }) {
    // indexBanner
    if (type === 1) {
      state.indexBannerList = data
    } else {
      state.creditBannerList = data
    }
  },
}

export default {
  state,
  actions,
  mutations,
}
